﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace PrinterMasterServer
{
    static class Log
    {
        public enum Type
        {
            error, warning, trys, debug
        }
        public enum Titel
        {
            文件读写异常,Socket异常,未知异常,debug信息
        }
        public static string Write(Type type,Titel titel,string text)
        {
            var str = string.Format("[{0}] - [{1} | {2}] - {3} \n",
                DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"),
                type.ToString(),titel.ToString(),text);
            //byte[] yq = Encoding.GetEncoding("gb2312").GetBytes(str);
            m_logWrite.WriteLine(str);
            m_logWrite.Flush();
            return str;
        }
        public static StreamWriter m_logWrite = null;
        public static FileStream m_logStream = null;
        public static void openLog()
        {
            //如果不存在，就创建它....
            if(!File.Exists(System.AppDomain.CurrentDomain.BaseDirectory+"log\\" + DateTime.Now.ToString("yyyyMMdd") + ".ini"))
                m_logStream = new FileStream("log\\" + DateTime.Now.ToString("yyyyMMdd") + ".ini", FileMode.OpenOrCreate, FileAccess.ReadWrite);
            else
                m_logStream = new FileStream("log\\" + DateTime.Now.ToString("yyyyMMdd") + ".ini", FileMode.Open ,FileAccess.ReadWrite);

            
            //m_logWrite.Encoding = Encoding.Unicode;
            m_logWrite = new StreamWriter(m_logStream,Encoding.Unicode); // 创建写入流
        }
        public static void closeLog()
        {
            m_logWrite.Close();
            m_logStream.Close();
        }
    }
}
